let wrapper = $("#wrapper");
let body = $("body");
!function ($) {
    "use strict";
    let Sidemenu = function () {
        this.$body = body;
        this.$openLeftBtn = $(".open-left");
        this.$menuItem = $("#sidebar-menu a");
    };
    Sidemenu.prototype.openLeftBar = function () {
        wrapper.toggleClass("enlarged");
        wrapper.addClass("forced");

        if (wrapper.hasClass("enlarged") && body.hasClass("fixed-left")) {
            body.removeClass("fixed-left").addClass("fixed-left-void");
        } else if (!wrapper.hasClass("enlarged") && body.hasClass("fixed-left-void")) {
            body.removeClass("fixed-left-void").addClass("fixed-left");
        }
        if (wrapper.hasClass("enlarged")) {
            $(".left ul").removeAttr("style");
        } else {
            $(".subdrop").siblings("ul:first").show();
        }
        toggle_slimscroll(".slimscrollleft");
        body.trigger("resize");
    };
    //menu item click
    Sidemenu.prototype.menuItemClick = function (e) {
        if (!wrapper.hasClass("enlarged")) {
            if ($(this).parent().hasClass("has_sub")) {
                e.preventDefault();
            }
            if (!$(this).hasClass("subdrop")) {
                // hide any open menus and remove all other classes
                $("ul", $(this).parents("ul:first")).slideUp(350);
                $("a", $(this).parents("ul:first")).removeClass("subdrop");
                $("#sidebar-menu .pull-right i").removeClass("md-remove").addClass("md-add");

                // open our new menu and add the open class
                $(this).next("ul").slideDown(350);
                $(this).addClass("subdrop");
                $(".pull-right i", $(this).parents(".has_sub:last")).removeClass("md-add").addClass("md-remove");
                $(".pull-right i", $(this).siblings("ul")).removeClass("md-remove").addClass("md-add");
            } else if ($(this).hasClass("subdrop")) {
                $(this).removeClass("subdrop");
                $(this).next("ul").slideUp(350);
                $(".pull-right i", $(this).parent()).removeClass("md-remove").addClass("md-add");
            }
        }
    };
    //init sidemenu
    Sidemenu.prototype.init = function () {
        let $this = this;
        //bind on click
        $(".open-left").click(function (e) {
            e.stopPropagation();
            $this.openLeftBar();
        });

        // LEFT SIDE MAIN NAVIGATION
        $this.$menuItem.on('click', $this.menuItemClick);

        // NAVIGATION HIGHLIGHT & OPEN PARENT
        $("#sidebar-menu ul li.has_sub a.active").parents("li:last").children("a:first").addClass("active").trigger("click");
    };
    //init Sidemenu
    $.Sidemenu = new Sidemenu;
    $.Sidemenu.Constructor = Sidemenu
    /*------------- admin ui init -------------*/
    let curPath = window.location.pathname;
    let oLis = $("#sidebar-menu>ul>li");
    for (let i = 0; i < oLis.length; i++) {
        let pLi = $(oLis[i]);
        console.log(pLi)
        let pClass = $(pLi).attr("class");
        if (pClass === "has_sub") {
            let oA = $(pLi).find("a");
        } else {
            let oA = $(pLi).find("a");
            let href = $(oA).attr("href");
            if (curPath.includes(href)) {
                $(oA).addClass("active");
                console.log($(oA).attr("class"))
                break;
            }
        }
    }
}(window.jQuery);


(function fullScreen($) {
    "use strict";

    let FullScreen = function () {
        this.$body = body;
        this.$fullscreenBtn = $("#btn-fullscreen");
    };
    //turn on full screen
    // Thanks to http://davidwalsh.name/fullscreen
    FullScreen.prototype.launchFullscreen = function (element) {
        if (element.requestFullscreen) {
            element.requestFullscreen();
        } else if (element.mozRequestFullScreen) {
            element.mozRequestFullScreen();
        } else if (element.webkitRequestFullscreen) {
            element.webkitRequestFullscreen();
        } else if (element.msRequestFullscreen) {
            element.msRequestFullscreen();
        }
    };
    FullScreen.prototype.exitFullscreen = function () {
        if (document.exitFullscreen) {
            document.exitFullscreen();
        } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        } else if (document.webkitExitFullscreen) {
            document.webkitExitFullscreen();
        }
    };
    //toggle screen
    FullScreen.prototype.toggle_fullscreen = function () {
        let $this = this;
        let fullscreenEnabled = document.fullscreenEnabled || document.mozFullScreenEnabled || document.webkitFullscreenEnabled;
        if (fullscreenEnabled) {
            if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) {
                $this.launchFullscreen(document.documentElement);
            } else {
                $this.exitFullscreen();
            }
        }
    };
    //init sidemenu
    FullScreen.prototype.init = function () {
        let $this = this;
        //bind
        $this.$fullscreenBtn.on('click', function () {
            $this.toggle_fullscreen();
        });
    };
    //init FullScreen
    $.FullScreen = new FullScreen;
    $.FullScreen.Constructor = FullScreen;

})(window.jQuery);

//portlets
(function ($) {
    "use strict";
    /**
     Portlet Widget
     */
    let Portlet = function () {
        this.$body = body,
            this.$portletIdentifier = ".portlet",
            this.$portletCloser = '.portlet a[data-toggle="remove"]',
            this.$portletRefresher = '.portlet a[data-toggle="reload"]'
    };
    //on init
    Portlet.prototype.init = function () {
        // Panel closest
        let $this = this;
        $(document).on("click", this.$portletCloser, function (ev) {
            ev.preventDefault();
            let $portlet = $(this).closest($this.$portletIdentifier);
            let $portlet_parent = $portlet.parent();
            $portlet.remove();
            if ($portlet_parent.children().length === 0) {
                $portlet_parent.remove();
            }
        });
        // Panel Reload
        $(document).on("click", this.$portletRefresher, function (ev) {
            ev.preventDefault();
            let $portlet = $(this).closest($this.$portletIdentifier);
            // This is just a simulation, nothing is going to be reloaded
            $portlet.append('<div class="panel-disabled"><div class="loader-1"></div></div>');
            let $pd = $portlet.find('.panel-disabled');
            setTimeout(function () {
                $pd.fadeOut('fast', function () {
                    $pd.remove();
                });
            }, 500 + 300 * (Math.random() * 5));
        });
    };
    //
    $.Portlet = new Portlet;
    $.Portlet.Constructor = Portlet;

})(window.jQuery);

//main app module
(function ($) {
    "use strict";

    let fileShareApp = function () {
        this.VERSION = "1.0.0";
        this.AUTHOR = "Geticsen";
        this.SUPPORT = "support@fileshare.com";
        this.pageScrollElement = "html, body";
        this.$body = body;
    };
    //initializing tooltip
    fileShareApp.prototype.initTooltipPlugin = function () {
        $.fn.tooltip && $('[data-toggle="tooltip"]').tooltip()
    };

    //initializing popover
    fileShareApp.prototype.initPopoverPlugin = function () {
        $.fn.popover && $('[data-toggle="popover"]').popover()
    };

    //initializing nicescroll
    fileShareApp.prototype.initNiceScrollPlugin = function () {
        //You can change the color of scroll bar here
        $.fn.niceScroll && $(".nicescroll").niceScroll({cursorcolor: '#9d9ea5', cursorborderradius: '0px'});
    };
    //initializing knob
    fileShareApp.prototype.initKnob = function () {
        let knob = $(".knob");
        if (knob.length > 0) {
            knob.knob();
        }
    };
    //on doc load
    fileShareApp.prototype.onDocReady = function (e) {
        FastClick.attach(document.body);
        resizeFunc.push("initScrolls");
        resizeFunc.push("changepType");
        $('.animate-number').each(function () {
            $(this).animateNumbers($(this).attr("data-value"), true, parseInt($(this).attr("data-duration")));
        });
        //RUN RESIZE ITEMS
        $(window).resize(debounce(resizeItems, 100));
        body.trigger("resize");
        // right side-bar toggle
        $('.right-bar-toggle').on('click', function (e) {
            e.preventDefault();
            $('#wrapper').toggleClass('right-bar-enabled');
        });
    };
    //initilizing
    fileShareApp.prototype.init = function () {
        let $this = this;
        this.initTooltipPlugin();
        this.initPopoverPlugin();
        this.initNiceScrollPlugin();
        this.initKnob();
        //document load initialization
        $(document).ready($this.onDocReady);
        //creating portles
        $.Portlet.init();
        //init side bar - left
        $.Sidemenu.init();
        //init fullscreen
        $.FullScreen.init();
    };
    $.fileShareApp = new fileShareApp;
    $.fileShareApp.Constructor = fileShareApp

})(window.jQuery);

//initializing main application module
(function ($) {
    "use strict";
    $.fileShareApp.init();
})(window.jQuery);


/* ------------ some utility functions ----------------------- */
//this full screen
let toggle_fullscreen = function () {

}

function executeFunctionByName(functionName, context /*, args */) {
    let args = [].slice.call(arguments).splice(2);
    let namespaces = functionName.split(".");
    let func = namespaces.pop();
    for (let i = 0; i < namespaces.length; i++) {
        context = context[namespaces[i]];
    }
    return context[func].apply(this, args);
}

let w, h, dw, dh;
window.changepType = function () {
    w = $(window).width();
    h = $(window).height();
    dw = $(document).width();
    dh = $(document).height();

    if (jQuery.browser.mobile === true) {
        body.addClass("mobile").removeClass("fixed-left");
    }

    if (!wrapper.hasClass("forced")) {
        if (w > 990) {
            body.removeClass("smallscreen").addClass("widescreen");
            wrapper.removeClass("enlarged");
        } else {
            body.removeClass("widescreen").addClass("smallscreen");
            wrapper.addClass("enlarged");
            $(".left ul").removeAttr("style");
        }
        if (wrapper.hasClass("enlarged") && body.hasClass("fixed-left")) {
            body.removeClass("fixed-left").addClass("fixed-left-void");
        } else if (!wrapper.hasClass("enlarged") && body.hasClass("fixed-left-void")) {
            body.removeClass("fixed-left-void").addClass("fixed-left");
        }

    }
    toggle_slimscroll(".slimscrollleft");
}


let debounce = function (func, wait, immediate) {
    let timeout, result;
    return function () {
        let context = this, args = arguments;
        let later = function () {
            timeout = null;
            if (!immediate) result = func.apply(context, args);
        };
        let callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) result = func.apply(context, args);
        return result;
    };
}

function resizeItems() {
    if ($.isArray(resizeFunc)) {
        for (i = 0; i < resizeFunc.length; i++) {
            window[resizeFunc[i]]();
        }
    }
}

window.initScrolls = function initScrolls() {
    if (jQuery.browser.mobile !== true) {
        //SLIM SCROLL
        $('.slimscroller').slimscroll({
            height: 'auto',
            size: "5px"
        });

        $('.slimscrollleft').slimScroll({
            height: 'auto',
            position: 'right',
            size: "5px",
            color: '#7A868F',
            wheelStep: 5
        });
    }
}

function toggle_slimscroll(item) {
    if (wrapper.hasClass("enlarged")) {
        $(item).css("overflow", "inherit").parent().css("overflow", "inherit");
        $(item).siblings(".slimScrollBar").css("visibility", "hidden");
    } else {
        $(item).css("overflow", "hidden").parent().css("overflow", "hidden");
        $(item).siblings(".slimScrollBar").css("visibility", "visible");
    }
}

let wow = new WOW(
    {
        boxClass: 'wow', // animated element css class (default is wow)
        animateClass: 'animated', // animation css class (default is animated)
        offset: 50, // distance to the element when triggering the animation (default is 0)
        mobile: false        // trigger animations on mobile devices (true is default)
    }
);
wow.init();
